#!/bin/bash


#####################
#### User Guide
#### run_ski_genlef.sh gdsname/oasisname verilog/cdl metal_max_layer netlistType(verilog cdl)
#####################

#### skipper PATH environment setting ####
## need to set
install_path="/eda/empyrean/skipper_genlef_2022.06.sp2_linux-x86_64_20231018"
# setenv is csh command not bash
# setenv PATH ${install_path}/bin:$PATH
export PATH=${install_path}/bin:$PATH

IFS=: DIRS=($PATH)
declare DIRS

a=1
for ELEM in "${DIRS[@]}"
do
	if [[ ${a} -eq 1 ]]; then
		#echo "$ELEM"
		echo "$ELEM" > ./env.path
        else
		#echo "$ELEM"
		echo "$ELEM" >> ./env.path
	fi
	let " a += 1"
done

#### run ####
## need to set
#run_dir="/projects/analog_ip/workspace/xwqiu/virtuoso/tacoma6/ski_genlef"
layermap="/users/xwqiu/scripts/ski_genlef/flow/layer.conf.T6"
option="/users/xwqiu/scripts/ski_genlef/flow/option.conf"
run_dir=$(pwd)
gds=$1
cellName=`exec drvTopcell ${gds}`
netlist=$2
top_metal=$3
netlist_type=$4



cp ./example_run.tcl ./arun.tcl

sed -i 's#GDS#'"${gds}"'#g' ./arun.tcl
sed -i 's#CELLNAME#'"${cellName}"'#g' ./arun.tcl
sed -i 's#LAYERMAP#'"${layermap}"'#g' ./arun.tcl
sed -i 's#NETLIST#'"${netlist}"'#g' ./arun.tcl
sed -i 's#TYPE#'"${netlist_type}"'#g' ./arun.tcl
sed -i 's#OPTION#'"${option}"'#g' ./arun.tcl
sed -i 's#TOPMETAL#'"${top_metal}"'#g' ./arun.tcl
sed -i 's#PWD#'"${run_dir}"'#g' ./arun.tcl

ski-genlef ${run_dir}/arun.tcl

mv ./*.lef ./output/${cellName}.lef

## clear_log
echo "clear_log"
mv lefSummary.log ${cellName}.history
rm -f ./*.log ./autoCrtTraceRule.rule ./autoSave.setting ./env.path ./run.option ./*.lef ./*.pin
rm -rf ./running
rm -f ./run.tcl